import box/{condit,logger,ui,tools}

Get-Proot-Container-Choose-Ui(){
  local MOD=$1
  if [ -z $2 ];then
    local menu=true __option=()
    local FIR_LINE=2
    local IF1=0 IF2=0
    for ((i=0;i<${#releases[@]};i++));do
      local _NAME=${releases[i]%/*}
      if Has-Dir $PL_HOME/$_NAME && Has-File $PL_HOME/start/$_NAME.sh;then
        local IF="√"
        let IF1++
      else
        local IF="×"
        let IF2++
      fi
      __option+=("[$IF] $_NAME \e[0m   \t│${readme[i]}")
      let FIR_LINE++
    done
    if In-Value MOD;then
      if ! ((IF2));then
        Info "已安装所有的发行版"
        exit
      fi
    else
      if ! ((IF1));then
        Info "无可移除的发行版"
        exit
      fi
    fi
    Get-List-Ui \
    --title="选择发行版,[]内代表安装状态" \
    --tip="按 ↑↓ 选择, 按 Ether 选中, 按 ESC 退出" \
    --print-choose --print-number --retract --beep \
    "${__option[@]}"
    Choose=$[__return__-1]
    ((__return__)) || { Info "退出";exit;}

    : ${Choose:=Null}
    RELEASE=${releases[Choose]}
    RELEASE_NAME=$(dirname $RELEASE)
    RELEASE_PATH=$PL_HOME/$RELEASE_NAME
    if ! Is-Num $Choose;then
      Err "非法的选项 $Choose"
      $FUNCNAME $@
      return
    elif ((Choose > ${#releases[@]}));then
      Err "过大的选择"
      $FUNCNAME $@
      return
    fi
  else
    local menu=false
    unset Choose
    Arg2=$2
    for ((i=0;i<${#releases[@]};i++));do
      Item=${releases[i]%/*}
      if In-Value Arg2 == Item;then
        Choose=$i
        RELEASE=${releases[Choose]}
        RELEASE_NAME=$(dirname $RELEASE)
        RELEASE_PATH=$PL_HOME/$RELEASE_NAME
        break
      fi
    done
    if [ -z $Choose ];then
      Err "错误的选项 $Arg2"
      exit
    fi
  fi
   local RP=false RN=false
   Has-Dir $RELEASE_PATH && RP=true
   Has-File $PL_HOME/start/$RELEASE_NAME.sh && RN=true
   if In-Value MOD;then
     if ($RP && $RN);then
       Err "$RELEASE_NAME: 该发行版已安装"
       if In-Value menu;then
         $FUNCNAME $@
         return
       else
         exit
       fi
     fi
   else
     if ! ($RP && $RN);then
       Err "$RELEASE_NAME: 该发行版尚未安装"
       if In-Value menu;then
         $FUNCNAME $@
         return
       else
         exit
       fi
     fi
   fi
}